<?php

use think\migration\Migrator;
use think\migration\db\Column;

class TimeStamps extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $tbPrefix = 'hpt_';
        $tables = [
            'game',
            'game_bet',
            'site_amount',
            'site_node',
//            'user_bet',
        ];

        foreach ($tables as $table) {
            $fullTable = $tbPrefix . $table;

            if ($this->table($table)->hasColumn('create_time')) {
                $this->execute('ALTER TABLE ' . $fullTable . ' DROP COLUMN create_time');
            }

            if ($this->table($table)->hasColumn('update_time')) {
                $this->execute('ALTER TABLE ' . $fullTable . ' DROP COLUMN update_time');
            }

            $tb = $this->table($table)
                ->addColumn('create_time', 'integer', ['signed' => false, 'default' => 0])
                ->addColumn('update_time', 'integer', ['signed' => false, 'default' => 0]);
            if ($table == 'user_bet'){
                if ($this->table($table)->hasColumn('query_time')) {
                    $this->execute('ALTER TABLE ' . $fullTable . ' DROP COLUMN query_time');
                }

                $tb->addColumn('query_time', 'integer', ['signed' => false, 'default' => 0]);
            }

            $tb->update();
        }
    }
}
